using Microsoft.EntityFrameworkCore;
using System;
using Terra.Data.Entities.Identity;

namespace Terra.Data.SeedData.IdentitySeedData;

/// <summary>
/// 菜单种子数据提供者
/// </summary>
public class MenuSeedData : ISeedDataProvider
{
    /// <summary>
    /// 系统内置菜单ID常量
    /// </summary>
    public static class SystemMenuIds
    {
        /// <summary>
        /// 仪表盘
        /// </summary>
        public static readonly Guid DashboardId = Guid.Parse("A94A1520-A6A2-4180-9FAC-9A901AD40AE0");
        public static readonly Guid AnalyticsId = Guid.Parse("B2781E73-E654-4128-9766-46E58EAF1062");
        public static readonly Guid WorkspaceId = Guid.Parse("C3BAFBCE-7836-4C31-91B2-84DBD0A0F161");

        /// <summary>
        /// 系统管理
        /// </summary>
        public static readonly Guid SystemId = Guid.Parse("D456B674-9E55-4BA4-BFF1-F7E6A4825146");
        public static readonly Guid MenuManagementId = Guid.Parse("E503B8F5-9FA6-4B4D-9AEB-EA1C1FB0C8E2");
        public static readonly Guid UserManagementId = Guid.Parse("F69A0EB6-B6C7-40B7-A0CA-0BDF68D4F448");
        public static readonly Guid RoleManagementId = Guid.Parse("A72A9DD9-8732-40C5-BCE1-9EA544FF3B04");
        public static readonly Guid DeptManagementId = Guid.Parse("B83BDDB0-9943-41AE-B62A-E8FE3BCBC832");

        /// <summary>
        /// 日志管理
        /// </summary>
        public static readonly Guid LogId = Guid.Parse("C94D352D-9654-428B-940D-D7FE35160ADF");
        public static readonly Guid LoginLogId = Guid.Parse("D05E14E5-8065-4D8B-9D3C-89E88EBF2B9C");
        public static readonly Guid OperationLogId = Guid.Parse("E16B7176-9D76-4A5D-8C6D-FA1E1E37C92D");

        /// <summary>
        /// 按钮ID
        /// </summary>
        public static class ButtonIds
        {
            // 菜单管理按钮
            public static readonly Guid MenuCreateId = Guid.Parse("F1B7D9B0-8C1A-4B5A-B91E-9C1E1E37C92D");
            public static readonly Guid MenuUpdateId = Guid.Parse("F2B7D9B0-8C1A-4B5A-B91E-9C1E1E37C92D");
            public static readonly Guid MenuDeleteId = Guid.Parse("F3B7D9B0-8C1A-4B5A-B91E-9C1E1E37C92D");

            // 用户管理按钮
            public static readonly Guid UserCreateId = Guid.Parse("F4B7D9B0-8C1A-4B5A-B91E-9C1E1E37C92D");
            public static readonly Guid UserUpdateId = Guid.Parse("F5B7D9B0-8C1A-4B5A-B91E-9C1E1E37C92D");
            public static readonly Guid UserDeleteId = Guid.Parse("F6B7D9B0-8C1A-4B5A-B91E-9C1E1E37C92D");

            // 角色管理按钮
            public static readonly Guid RoleCreateId = Guid.Parse("F7B7D9B0-8C1A-4B5A-B91E-9C1E1E37C92D");
            public static readonly Guid RoleUpdateId = Guid.Parse("F8B7D9B0-8C1A-4B5A-B91E-9C1E1E37C92D");
            public static readonly Guid RoleDeleteId = Guid.Parse("F9B7D9B0-8C1A-4B5A-B91E-9C1E1E37C92D");

            // 部门管理按钮
            public static readonly Guid DeptCreateId = Guid.Parse("FAB7D9B0-8C1A-4B5A-B91E-9C1E1E37C92D");
            public static readonly Guid DeptUpdateId = Guid.Parse("FBB7D9B0-8C1A-4B5A-B91E-9C1E1E37C92D");
            public static readonly Guid DeptDeleteId = Guid.Parse("FCB7D9B0-8C1A-4B5A-B91E-9C1E1E37C92D");

            // 日志管理按钮
            public static readonly Guid LoginLogCleanId = Guid.Parse("FDB7D9B0-8C1A-4B5A-B91E-9C1E1E37C92D");
            public static readonly Guid OperationLogCleanId = Guid.Parse("FEB7D9B0-8C1A-4B5A-B91E-9C1E1E37C92D");
        }
    }

    private readonly DateTime _now = DateTime.UtcNow;
    private readonly Guid _adminId = UserSeedData.SystemUserIds.AdminUserId;

    /// <summary>
    /// 创建菜单
    /// </summary>
    private Menu CreateMenu(
        Guid id,
        string name,
        string title,
        string type,
        string path,
        string component,
        string icon,
        string authCode,
        int sort,
        Guid? parentId = null,
        bool keepAlive = false,
        bool affixTab = false,
        string badge = null,
        string badgeType = null,
        string badgeVariants = null)
    {
        return new Menu
        {
            Id = id,
            ParentId = parentId,
            Name = name,
            Title = title,
            Type = type,
            Path = path,
            Component = component,
            Icon = icon,
            AuthCode = authCode,
            Status = 1,
            KeepAlive = keepAlive,
            AffixTab = affixTab,
            Sort = sort,
            Badge = badge,
            BadgeType = badgeType,
            BadgeVariants = badgeVariants,
            CreatedTime = _now,
            CreatedBy = _adminId,
            LastModifiedTime = _now,
            LastModifiedBy = _adminId,
            IsDeleted = false
        };
    }

    /// <summary>
    /// 配置菜单种子数据
    /// </summary>
    /// <param name="modelBuilder">模型构建器</param>
    public void Configure(ModelBuilder modelBuilder)
    {
        #region 仪表盘菜单

        // 仪表盘目录
        modelBuilder.Entity<Menu>().HasData(CreateMenu(
            SystemMenuIds.DashboardId, "Dashboard", "page.dashboard.title", "catalog", "/dashboard",
            "LAYOUT", "carbon:dashboard", "menu:dashboard", 10, null, true));

        // 数据分析
        modelBuilder.Entity<Menu>().HasData(CreateMenu(
            SystemMenuIds.AnalyticsId, "Analytics", "page.dashboard.analytics", "menu", "/dashboard/analytics",
            "/dashboard/analytics/index", "carbon:analytics", "menu:dashboard:analytics", 11, SystemMenuIds.DashboardId, true, true));

        // 工作台
        modelBuilder.Entity<Menu>().HasData(CreateMenu(
            SystemMenuIds.WorkspaceId, "Workspace", "page.dashboard.workspace", "menu", "/dashboard/workspace",
            "/dashboard/workspace/index", "carbon:workspace", "menu:dashboard:workspace", 12, SystemMenuIds.DashboardId, true));

        #endregion

        #region 系统管理菜单

        // 系统管理目录
        modelBuilder.Entity<Menu>().HasData(CreateMenu(
            SystemMenuIds.SystemId, "System", "system.title", "catalog", "/system", "LAYOUT",
            "carbon:settings", "menu:system", 90, null, false, false, "new", "normal", "primary"));

        // 菜单管理
        modelBuilder.Entity<Menu>().HasData(CreateMenu(
            SystemMenuIds.MenuManagementId, "SystemMenu", "system.menu.title", "menu", "/system/menu",
            "/system/menu/list", "carbon:menu", "menu:system:menu", 91, SystemMenuIds.SystemId));

        // 菜单管理按钮
        modelBuilder.Entity<Menu>().HasData(
            CreateMenu(SystemMenuIds.ButtonIds.MenuCreateId, "MenuCreate", "system.menu.create", "button", null,
                null, "carbon:add", "menu:system:menu:create", 1, SystemMenuIds.MenuManagementId),
            CreateMenu(SystemMenuIds.ButtonIds.MenuUpdateId, "MenuUpdate", "system.menu.update", "button", null,
                null, "carbon:edit", "menu:system:menu:update", 2, SystemMenuIds.MenuManagementId),
            CreateMenu(SystemMenuIds.ButtonIds.MenuDeleteId, "MenuDelete", "system.menu.delete", "button", null,
                null, "carbon:delete", "menu:system:menu:delete", 3, SystemMenuIds.MenuManagementId)
        );

        // 用户管理
        modelBuilder.Entity<Menu>().HasData(CreateMenu(
            SystemMenuIds.UserManagementId, "SystemUser", "system.user.title", "menu", "/system/user",
            "/system/user/list", "carbon:user", "menu:system:user", 92, SystemMenuIds.SystemId));

        // 用户管理按钮
        modelBuilder.Entity<Menu>().HasData(
            CreateMenu(SystemMenuIds.ButtonIds.UserCreateId, "UserCreate", "system.user.create", "button", null,
                null, "carbon:add", "menu:system:user:create", 1, SystemMenuIds.UserManagementId),
            CreateMenu(SystemMenuIds.ButtonIds.UserUpdateId, "UserUpdate", "system.user.update", "button", null,
                null, "carbon:edit", "menu:system:user:update", 2, SystemMenuIds.UserManagementId),
            CreateMenu(SystemMenuIds.ButtonIds.UserDeleteId, "UserDelete", "system.user.delete", "button", null,
                null, "carbon:delete", "menu:system:user:delete", 3, SystemMenuIds.UserManagementId)
        );

        // 角色管理
        modelBuilder.Entity<Menu>().HasData(CreateMenu(
            SystemMenuIds.RoleManagementId, "SystemRole", "system.role.title", "menu", "/system/role",
            "/system/role/list", "carbon:user-role", "menu:system:role", 93, SystemMenuIds.SystemId));

        // 角色管理按钮
        modelBuilder.Entity<Menu>().HasData(
            CreateMenu(SystemMenuIds.ButtonIds.RoleCreateId, "RoleCreate", "system.role.create", "button", null,
                null, "carbon:add", "menu:system:role:create", 1, SystemMenuIds.RoleManagementId),
            CreateMenu(SystemMenuIds.ButtonIds.RoleUpdateId, "RoleUpdate", "system.role.update", "button", null,
                null, "carbon:edit", "menu:system:role:update", 2, SystemMenuIds.RoleManagementId),
            CreateMenu(SystemMenuIds.ButtonIds.RoleDeleteId, "RoleDelete", "system.role.delete", "button", null,
                null, "carbon:delete", "menu:system:role:delete", 3, SystemMenuIds.RoleManagementId)
        );

        // 部门管理
        modelBuilder.Entity<Menu>().HasData(CreateMenu(
            SystemMenuIds.DeptManagementId, "SystemDept", "system.dept.title", "menu", "/system/dept",
            "/system/dept/list", "carbon:organization", "menu:system:dept", 94, SystemMenuIds.SystemId));

        // 部门管理按钮
        modelBuilder.Entity<Menu>().HasData(
            CreateMenu(SystemMenuIds.ButtonIds.DeptCreateId, "DeptCreate", "system.dept.create", "button", null,
                null, "carbon:add", "menu:system:dept:create", 1, SystemMenuIds.DeptManagementId),
            CreateMenu(SystemMenuIds.ButtonIds.DeptUpdateId, "DeptUpdate", "system.dept.update", "button", null,
                null, "carbon:edit", "menu:system:dept:update", 2, SystemMenuIds.DeptManagementId),
            CreateMenu(SystemMenuIds.ButtonIds.DeptDeleteId, "DeptDelete", "system.dept.delete", "button", null,
                null, "carbon:delete", "menu:system:dept:delete", 3, SystemMenuIds.DeptManagementId)
        );

        #endregion

        #region 日志管理菜单

        // 日志管理目录
        modelBuilder.Entity<Menu>().HasData(CreateMenu(
            SystemMenuIds.LogId, "Log", "log.title", "catalog", "/log", "LAYOUT",
            "carbon:document", "menu:log", 95, SystemMenuIds.SystemId));

        // 登录日志
        modelBuilder.Entity<Menu>().HasData(CreateMenu(
            SystemMenuIds.LoginLogId, "LoginLog", "log.login.title", "menu", "/log/login",
            "/log/login/list", "carbon:login", "menu:log:login", 96, SystemMenuIds.LogId));

        // 登录日志按钮
        modelBuilder.Entity<Menu>().HasData(
            CreateMenu(SystemMenuIds.ButtonIds.LoginLogCleanId, "LoginLogClean", "log.login.clean", "button", null,
                null, "carbon:clean", "menu:log:login:clean", 1, SystemMenuIds.LoginLogId)
        );

        // 操作日志
        modelBuilder.Entity<Menu>().HasData(CreateMenu(
            SystemMenuIds.OperationLogId, "OperationLog", "log.operation.title", "menu", "/log/operation",
            "/log/operation/list", "carbon:data-view-alt", "menu:log:operation", 97, SystemMenuIds.LogId));

        // 操作日志按钮
        modelBuilder.Entity<Menu>().HasData(
            CreateMenu(SystemMenuIds.ButtonIds.OperationLogCleanId, "OperationLogClean", "log.operation.clean", "button", null,
                null, "carbon:clean", "menu:log:operation:clean", 1, SystemMenuIds.OperationLogId)
        );

        #endregion
    }

    /// <summary>
    /// 获取提供者优先级（在角色种子数据之后）
    /// </summary>
    public int Order => 30;
}